<template>
    <div class="app-container">
        <div :class="['filter-container']">

            <!-- 筛选框需要些双份 一种是没有展开状态下的  只有主要的几个筛选框，另一种是全部的筛选框都展示 也就是展开状态下的 -->
            <!-- 没有展开状态下的筛选框 -->
            <el-form ref="filtrateForm" :inline="true" :model="filtrateForm" label-width="140px">
                <el-form-item label="银行名称/银行账户:">
                    <el-input v-model.trim="filtrateForm.name" placeholder="请输入" style="width:160px;" clearable />
                </el-form-item>
                <el-form-item>
                    <el-button @click="getList()">查询</el-button>
                    <el-button v-permission="['sys:config:save']" type="primary" class="filter-item" style="margin-left: 10px;"
                        @click="handleCreate()">新增</el-button>
                    <!-- <el-button v-permission="['sys:config:delete']" type="danger" :disabled="dataListSelections.length <= 0"
                        @click="deleteHandle()">批量删除</el-button> -->
                </el-form-item>

            </el-form>

        </div>

        <div :class="['table']">
            <div class="table-container">
                <el-table :key="tableKey" v-loading="listLoading" row-key="menuId" height="100%" :data="list" border fit
                    highlight-current-row style="width: 100%;" @selection-change="selectionChangeHandle">

                    <el-table-column type="selection" header-align="center" align="center" width="50" />

                    <el-table-column prop="bankName" header-align="center" align="center" label="银行名称" />
                    <el-table-column prop="accountNumber" header-align="center" align="center" label="银行账号" />
                    <el-table-column prop="bankAddress" header-align="center" align="center" label="开户行" />
                    <el-table-column prop="accountName" header-align="center" align="center" label="开户姓名" />
                    <el-table-column prop="remark" header-align="center" align="center" label="备注" />
                    <el-table-column prop="updateTime" header-align="center" align="center" width="180" label="最后编辑时间">
                        <template slot-scope="scope">{{ scope.row.updateTime | toData }}</template>
                    </el-table-column>
                    <el-table-column prop="updateUserName" header-align="center" align="center" label="编辑人" />
                    <el-table-column header-align="center" align="center" width="150" label="操作">
                        <template slot-scope="scope">
                            <el-button v-permission="['sys:config:update']" type="text" size="small" @click="handleUpdate(scope.row)">
                                修改
                            </el-button>
                            <el-button v-permission="['sys:config:delete']" type="text" size="small" @click="deleteHandle(scope.row)">删除
                            </el-button>
                        </template>
                    </el-table-column>

                </el-table>
            </div>
        </div>

        <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

        <!-- //新增或者修改弹框 -->
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @getList="handleFilter" />

    </div>
</template>

<script>
import { getAccountList, daleteAccount } from '@/api/assets'// 请求数据
import permission from '@/directive/permission/index.js' // 权限判断指令
import checkPermission from '@/utils/permission' // 权限判断函数
import { toDate } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination 分页组件
import AddOrUpdate from './add-or-update' // 新增或者修改弹框

export default {
    name: 'LayoutTable',
    components: { Pagination, AddOrUpdate },
    directives: { permission }, // 指令
    // 过滤
    filters: {
        toData (data) {
            return toDate(data)
        }
    },
    data () {
        return {
            // 筛选框的 form 表单
            filtrateForm: {
                name: ''
            },
            // 表单验证
            rules: {
            },
            dataListSelections: [], // 批量删除选中
            tableKey: 0,
            listLoading: true, // 表格加载数据时的 loading
            list: [], // 表格数据
            total: 0, // 总条数
            listQuery: { // 表格当前页 和 当前页的展示数量
                page: 1,
                limit: 20
            },
            addOrUpdateVisible: false // 新增和修改 弹框展示隐藏
        }
    },
    created () {
        this.getList()
    },
    methods: {
        // 获取表格数据
        async getList () {
            this.listLoading = true
            // 筛选+分页
            const params = Object.assign({
                accountInfo: this.filtrateForm.name
            }, this.listQuery)
            // 请求
            const res = await getAccountList(params).catch(e => { })
            // 失败
            if (!res) { this.listLoading = false; return }
            // 成功
            const { data, code, message } = res
            console.log(data)
            // data.list.forEach(item => {
            //     item.detailAddress = `${item.province}/${item.city}/${item.county}`
            //     if (!item.allHouses) {
            //         item.rentedOrAll = '--'
            //         return
            //     }
            //     item.rentedOrAll = Math.round(item.allHouses / item.rentedHouses * 100) + '%'
            // })
            this.list = data.list
            this.total = data.totalCount
            this.listLoading = false
        },
        // 重新获取表格数据 页码归为1
        handleFilter () {
            this.listQuery.page = 1
            this.getList()
        },
        // 新增数据 -- 打开弹框
        handleCreate () {
            this.addOrUpdateVisible = true
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init('create')
            })
        },
        // 修改数据 -- 打开弹框
        handleUpdate (row) {
            this.addOrUpdateVisible = true
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init('update', row)
            })
        },
        // 批量删除数据 - 多选
        selectionChangeHandle (val) {
            this.dataListSelections = val
        },
        // 删除数据 批量删除的话 不用传参
        deleteHandle (data) {
            const idS = data ? [data.id] : this.dataListSelections.map(item => { return item.id })
            const nameS = data ? [data.name] : this.dataListSelections.map(item => { return item.name })
            this.$confirm(`确定对[${nameS.join('、')}]进行[删除]操作?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                // 提交删除
                const { code, message } = await daleteAccount({}, idS)
                // 成功
                this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                        this.handleFilter()// 刷新数据
                    }
                })
            })
        }

    }
}
</script>
<style lang="scss" scoped>
//表格布局的注释：
//$rowLength 这个变量表示了 筛选的搜索框会有多少行，由于在不同宽度显示器下 所占行数会不一定 ，所以下面使用媒体查询 做兼容
//最小兼容至 1366
@import '~@/styles/layout-table.scss';
$rowLength: 2;
@media all and (min-width: 1659px) {
    $rowLength: 2;
    @include calculation($rowLength: $rowLength);
}
@media all and (max-width: 1659px) {
    $rowLength: 3;
    @include calculation($rowLength: $rowLength);
}
</style>
